<template>
  <div class="app-container">

    <el-form :model="formLabelAlign" label-position="right" label-width="80px">

      <el-form-item label="讲师头像">
        <el-upload :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" :action="devURl" :on-preview="handlePreview" class="avatar-uploader">
          <img v-if="formLabelAlign.avatar" ref="tx_image" :src="formLabelAlign.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>

      <el-form-item label="讲师名称">
        <el-input v-model="formLabelAlign.name" />
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="formLabelAlign.sort" :min="0" :max="10" :step="1" controls-position="right" />
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="formLabelAlign.intro" type="textarea" />
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="formLabelAlign.career" type="textarea" />
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="formLabelAlign.level" placeholder="选择讲师头衔">
          <el-option v-for="(level,index) in levels" :key="index" :label="level.label" :value="level.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">{{ isSave?"保存数据":"更新数据" }}</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import teacher from '@/api/edu/teacher.js'
export default {
  data() {
    return {
      devURl: process.env.BASE_API + '/qiNiuYun/uploadImage',
      levels: [
        { label: '高级讲师', value: 1 },
        { label: '首席讲师', value: 2 }
      ],
      formLabelAlign: {
        name: undefined, // 讲师名字
        intro: 1, // 讲师简介
        career: undefined, // 讲师资历
        level: undefined, // 头衔
        sort: undefined, // 排序
        avatar: undefined
      },
      saveBtnDisabled: false,
      isSave: true
    }
  },
  watch: {
    $route(to, from) {
      if (to.query.id == null) {
        this.formLabelAlign = {

        }
      }
    }
  },

  created() {
    if (this.$route.query.id) {
      // 这是编辑功能
      this.getTeacherId()
      this.isSave = false
    } else {
      this.formLabelAlign = {}
    }
    console.log(this.formLabelAlign)
  },
  methods: {

    openFileInput() {
      // 打开文件选择对话框
      this.$refs.fileInput.click()
    },
    saveOrUpdate() {
      if (this.isSave) {
        // 保存操作
        teacher.saveTeacher(this.formLabelAlign).then(result => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        })
      } else {
        // 更新操作
        teacher.updateTeacher(this.formLabelAlign).then(result => {
          this.$message({
            message: '更新成功',
            type: 'success'
          })
        })
      }
      this.disableButton()
      this.$router.push({ path: '/teacher/table' })
    },
    // 控制按钮的是否可用.
    disableButton() {
      this.saveBtnDisabled = true
    },
    // 编辑数据的数据回显.
    getTeacherId() {
      teacher.getTeacherId(this.$route.query.id).then(res => {
        this.formLabelAlign = res.data.teacher
        console.log(this.formLabelAlign)
      })
    },
    handlePreview() {},
    handleAvatarSuccess(res, file) {
      // 普通的js赋值有坑,没有响应式
      // this.formLabelAlign.avatar = res.data.url
      // console.log(this.formLabelAlign)
      // vue赋值有响应式
      this.$set(this.formLabelAlign, 'avatar', res.data.url)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
